#include <rawfile/raw_dir.h>
#include <string>
#include "cangjie_code_table.h"
#include "napi/native_api.h"
#include "rawfile/raw_file_manager.h"

static napi_value MatchCodeInTable(napi_env env, napi_callback_info info)
{
    size_t argc = 1;
    napi_value args[1] = {nullptr};
    
    napi_get_cb_info(env, info, &argc, args, nullptr, nullptr);
    
    napi_valuetype valuetype0;
    napi_typeof(env, args[0], &valuetype0);
    // TODO check value type
    
    char buffer[1024];
    size_t result;
    napi_get_value_string_utf8(env, args[0], buffer, sizeof(buffer), &result);
    // TODO check result

    std::string ch;
    
    napi_value matched;
    napi_create_string_utf8(env, ch.c_str(), ch.size(), &matched);
    return matched;
}

static napi_value SetCodeTablePath(napi_env env, napi_callback_info info)
{
    size_t argc = 1;
    napi_value args[1] = {nullptr};
    
    napi_get_cb_info(env, info, &argc, args, nullptr, nullptr);
    
    napi_valuetype valuetype0;
    napi_typeof(env, args[0], &valuetype0);
    // TODO check value type
    
    char buffer[1024];
    size_t result;
    napi_get_value_string_utf8(env, args[0], buffer, sizeof(buffer), &result);
    // TODO check result
    
    CangjieCodeTable::Instance().SetCodeTablePath(buffer);
    
    napi_value ret;
    napi_create_string_utf8(env, buffer, sizeof(buffer), &ret);
    return ret;
}

static napi_value Deploy(napi_env env, napi_callback_info info)
{
    size_t argc = 1;
    napi_value args[1] = {nullptr};
    
    napi_get_cb_info(env, info, &argc, args, nullptr, nullptr);
    
    napi_valuetype valuetype0;
    napi_typeof(env, args[0], &valuetype0);
    // TODO check value type
    
    char buffer[1024];
    size_t result;
    napi_get_value_string_utf8(env, args[0], buffer, sizeof(buffer), &result);
    // TODO check result
    
    std::string msg = CangjieCodeTable::Instance().Deploy(buffer);
    napi_value ret;
    napi_create_string_utf8(env, msg.data(), msg.size(), &ret);
    return ret;
}

static napi_value Load(napi_env env, napi_callback_info info)
{
    size_t argc = 1;
    napi_value args[1] = {nullptr};
    
    napi_get_cb_info(env, info, &argc, args, nullptr, nullptr);
    
    napi_valuetype valuetype0;
    napi_typeof(env, args[0], &valuetype0);
    // TODO check value type
    
    char buffer[1024];
    size_t result;
    napi_get_value_string_utf8(env, args[0], buffer, sizeof(buffer), &result);
    // TODO check result
    
    std::string msg = CangjieCodeTable::Instance().Load(buffer);
    napi_value ret;
    napi_create_string_utf8(env, msg.data(), msg.size(), &ret);
    return ret;
}

static napi_value CodeToChar(napi_env env, napi_callback_info info)
{
    size_t argc = 1;
    napi_value args[1] = {nullptr};
    
    napi_get_cb_info(env, info, &argc, args, nullptr, nullptr);
    
    napi_valuetype valuetype0;
    napi_typeof(env, args[0], &valuetype0);
    // TODO check value type
    
    char buffer[1024];
    size_t result;
    napi_get_value_string_utf8(env, args[0], buffer, sizeof(buffer), &result);
    // TODO check result
    
    std::string ch = CangjieCodeTable::Instance().CodeToChar(buffer);
    
    napi_value ret;
    napi_create_string_utf8(env, ch.c_str(), ch.size(), &ret);
    return ret;
}

static napi_value GetCandidates(napi_env env, napi_callback_info info)
{
    size_t argc = 1;
    napi_value args[1] = {nullptr};
    
    napi_get_cb_info(env, info, &argc, args, nullptr, nullptr);
    
    napi_valuetype valuetype0;
    napi_typeof(env, args[0], &valuetype0);
    // TODO check value type
    
    char buffer[1024];
    size_t result;
    napi_get_value_string_utf8(env, args[0], buffer, sizeof(buffer), &result);
    // TODO check result
    
    auto candidates = CangjieCodeTable::Instance().GetCandidates(buffer);
    
    napi_value candidateArray;
    napi_create_array(env, &candidateArray);
    for (std::size_t i = 0; i < candidates.size(); ++i) {
        napi_value candidate;
        napi_create_string_utf8(env, candidates[i].data(), candidates[i].size(), &candidate);
        napi_set_element(env, candidateArray, i, candidate);
    }
    return candidateArray;
}

static napi_value LoadFromCodeTableBuffer(napi_env env, napi_callback_info info)
{
    static bool loaded = false;
    if (loaded) {
        std::string msg = "code table is already loaded";
        napi_value ret;
        napi_create_string_utf8(env, msg.c_str(), msg.size(), &ret);
        return ret;
    }
    
    size_t argc = 1;
    napi_value args[1] = {nullptr};
    
    napi_get_cb_info(env, info, &argc, args, nullptr, nullptr);
    
    napi_valuetype valuetype0;
    napi_typeof(env, args[0], &valuetype0);
    // TODO check value type
    
    NativeResourceManager *manager = OH_ResourceManager_InitNativeResourceManager(env, args[0]);
    RawFile *rawFile = OH_ResourceManager_OpenRawFile(manager, "cangjie6.dict.yaml");
    long len = OH_ResourceManager_GetRawFileSize(rawFile);
    std::unique_ptr<char[]> data = std::make_unique<char[]>(len);
    int res = OH_ResourceManager_ReadRawFile(rawFile, data.get(), len);
    OH_ResourceManager_CloseRawFile(rawFile);
    OH_ResourceManager_ReleaseNativeResourceManager(manager);
    
    std::string msg = CangjieCodeTable::Instance().LoadFromCodeTableBuffer(data.get());
    loaded = true;
    
    napi_value ret;
    napi_create_string_utf8(env, msg.c_str(), msg.size(), &ret);
    return ret;
}

static napi_value LoadFromWordDictBuffer(napi_env env, napi_callback_info info)
{
    static bool loaded = false;
    if (loaded) {
        std::string msg = "word dict is already loaded";
        napi_value ret;
        napi_create_string_utf8(env, msg.c_str(), msg.size(), &ret);
        return ret;
    }
    
    size_t argc = 1;
    napi_value args[1] = {nullptr};
    
    napi_get_cb_info(env, info, &argc, args, nullptr, nullptr);
    
    napi_valuetype valuetype0;
    napi_typeof(env, args[0], &valuetype0);
    // TODO check value type
    
    NativeResourceManager *manager = OH_ResourceManager_InitNativeResourceManager(env, args[0]);
    RawFile *rawFile = OH_ResourceManager_OpenRawFile(manager, "cangjie6.extsimp.dict.yaml");
    long len = OH_ResourceManager_GetRawFileSize(rawFile);
    std::unique_ptr<char[]> data = std::make_unique<char[]>(len);
    int res = OH_ResourceManager_ReadRawFile(rawFile, data.get(), len);
    OH_ResourceManager_CloseRawFile(rawFile);
    OH_ResourceManager_ReleaseNativeResourceManager(manager);
    
    std::string msg = CangjieCodeTable::Instance().LoadFromWordDictBuffer(data.get());
    loaded = true;
    
    napi_value ret;
    napi_create_string_utf8(env, msg.c_str(), msg.size(), &ret);
    return ret;
}

EXTERN_C_START
static napi_value Init(napi_env env, napi_value exports)
{
    napi_property_descriptor desc[] = {
        { "matchCodeInTable", nullptr, MatchCodeInTable, nullptr, nullptr, nullptr, napi_default, nullptr },
        { "setCodeTablePath", nullptr, SetCodeTablePath, nullptr, nullptr, nullptr, napi_default, nullptr },
        { "deploy", nullptr, Deploy, nullptr, nullptr, nullptr, napi_default, nullptr },
        { "load", nullptr, Load, nullptr, nullptr, nullptr, napi_default, nullptr },
        { "codeToChar", nullptr, CodeToChar, nullptr, nullptr, nullptr, napi_default, nullptr },
        { "getCandidates", nullptr, GetCandidates, nullptr, nullptr, nullptr, napi_default, nullptr },
        { "loadFromCodeTableBuffer", nullptr, LoadFromCodeTableBuffer, nullptr, nullptr, nullptr, napi_default, nullptr },
        { "loadFromWordDictBuffer", nullptr, LoadFromWordDictBuffer, nullptr, nullptr, nullptr, napi_default, nullptr },
    };
    napi_define_properties(env, exports, sizeof(desc) / sizeof(desc[0]), desc);
    return exports;
}
EXTERN_C_END

static napi_module demoModule = {
    .nm_version = 1,
    .nm_flags = 0,
    .nm_filename = nullptr,
    .nm_register_func = Init,
    .nm_modname = "entry",
    .nm_priv = ((void*)0),
    .reserved = { 0 },
};

extern "C" __attribute__((constructor)) void RegisterEntryModule(void)
{
    napi_module_register(&demoModule);
}
